package org.dromara.solonplugins.zbus.rpc;

import java.util.LinkedList;
import java.util.List;

import org.noear.solon.Utils;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.AopContext;
import org.noear.solon.core.Props;
import org.noear.solon.core.util.ResourceUtil;

import io.zbus.mq.Broker;
import io.zbus.mq.BrokerConfig;
import io.zbus.rpc.Remote;

public class ZbusServiceBean {

	
	public static void reg(AopContext context) {
		
	}
	
	
	//zbusHttpServiceBootstrap
	private io.zbus.rpc.bootstrap.http.ServiceBootstrap createHttpServiceBootstrap(AopContext context) {
		io.zbus.rpc.bootstrap.http.ServiceBootstrap bootstrap = new io.zbus.rpc.bootstrap.http.ServiceBootstrap();
		Props props = context.cfg();
		String port = props.get("zbus.rpc.service.port");
		if (Utils.isNotBlank(port)) {
			bootstrap.port(Integer.parseInt(port));
		}
		String host = props.get("zbus.rpc.service.host");
		if (Utils.isNotBlank(host)) {
			bootstrap.host(host);
		}
		String certFile = props.get("zbus.rpc.service.certFile");
		if (Utils.isNotBlank(certFile)) {
			bootstrap.ssl(certFile, props.get("zbus.rpc.service.keyFile"));
		}
		String token = props.get("zbus.rpc.service.serviceToken");
		if (Utils.isNotBlank(token)) {
			bootstrap.serviceToken(token);
		}
		String responseTypeInfo = props.get("zbus.rpc.service.responseTypeInfo");
		if (Utils.isNotBlank(responseTypeInfo)) {
			bootstrap.responseTypeInfo(Boolean.parseBoolean(responseTypeInfo));
		}
		String verbose = props.get("zbus.rpc.service.verbose");
		if (Utils.isNotBlank(verbose)) {
			bootstrap.verbose(Boolean.parseBoolean(verbose));
		}
		bootstrap.autoDiscover(false); // 基于File的查找机制... 只能禁用
		for (String pkgName : splitIgnoreBlank(props.get("zbus.rpc.service.packageNames", ""), ",")) {
			for (Class<?> klass : ResourceUtil.scanClasses(pkgName)) {
				if (klass.getAnnotation(Component.class) != null && klass.getAnnotation(Remote.class) != null) {
					bootstrap.addModule(context.getBean(klass));
				}
			}
		}
		return bootstrap;
	}
	
	//zbusMqServiceBootstrap
	private io.zbus.rpc.bootstrap.mq.ServiceBootstrap createMqServiceBootstrap(AopContext context) {
		io.zbus.rpc.bootstrap.mq.ServiceBootstrap bootstrap = new io.zbus.rpc.bootstrap.mq.ServiceBootstrap();
		
		Props conf = context.cfg();
		bootstrap.serviceName(conf.get("zbus.rpc.service.serviceName"));
		
		String port = conf.get("zbus.rpc.service.port");
		if (Utils.isNotBlank(port)) {
			bootstrap.port(Integer.parseInt(port));
			String host = conf.get("zbus.rpc.service.host");
			if (Utils.isNotBlank(host)) {
				bootstrap.host(host);
			}
		} else {
			Broker zb = new Broker((BrokerConfig)context.getBean("zbusBrokerConfig"));
			bootstrap.broker(zb);
		}
		String certFile = conf.get("zbus.rpc.service.certFile");
		if (Utils.isNotBlank(certFile)) {
			bootstrap.ssl(certFile, conf.get("zbus.rpc.service.keyFile"));
		}
		String serviceToken = conf.get("zbus.rpc.service.token");
		if (Utils.isNotBlank(serviceToken)) {
			bootstrap.serviceToken(serviceToken);
		}
		String responseTypeInfo = conf.get("zbus.rpc.service.responseTypeInfo");
		if (Utils.isNotBlank(responseTypeInfo)) {
			bootstrap.responseTypeInfo(Boolean.parseBoolean(responseTypeInfo));
		}
		String verbose = conf.get("zbus.rpc.service.verbose");
		if (Utils.isNotBlank(verbose)) {
			bootstrap.verbose(Boolean.parseBoolean(verbose));
		}
		
		bootstrap.autoDiscover(false); // 基于File的查找机制... 只能禁用
		for (String pkgName : splitIgnoreBlank(conf.get("zbus.rpc.service.packageNames", ""),",")) {
			for (Class<?> klass : ResourceUtil.scanClasses(pkgName)) {
				if (klass.getAnnotation(Component.class) != null && klass.getAnnotation(Remote.class) != null) {
					bootstrap.addModule(context.getBean(klass));
				}
			}
		}
		return bootstrap; 
	}
	
	//zbusServiceBootstrap
	private ZbusServiceBootstrap createZBusServiceBootstrap(AopContext context) {
		ZbusServiceBootstrap bootstrap = new ZbusServiceBootstrap();
		Props conf = context.cfg();
		if ("mq".equals(conf.get("zbus.rpc.service.mode", "mq"))) {
			bootstrap.mq = context.getBean( "zbusMqServiceBootstrap");
		} else {
			bootstrap.http = context.getBean("zbusHttpServiceBootstrap");
		}
		return bootstrap;
	}
	
	
    /**
     * 根据一个正则式，将字符串拆分成数组，空元素将被忽略
     *
     * @param s
     *            字符串
     * @param regex
     *            正则式
     * @return 字符串数组
     */
    private static String[] splitIgnoreBlank(String s, String regex) {
        if (null == s)
            return null;
        String[] ss = s.split(regex);
        List<String> list = new LinkedList<String>();
        for (String st : ss) {
            if (Utils.isBlank(st))
                continue;
            list.add(st.trim());
        }
        return list.toArray(new String[list.size()]);
    }
}
